package com.xingdata.jjxc.activity;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.ProgressDialog;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import com.xingdata.jjxc.R;

/**
 * 视频播放器
 * 
 * @author Administrator
 * 
 */
public class VideoPlayActivity extends Activity implements
		SurfaceHolder.Callback, OnClickListener {
	private String TAG = "VideoPlayActivity";
	private MediaPlayer player;
	private SurfaceView surface;
	private SurfaceHolder surfaceHolder;
	private Button play, pause, stop;
	private String video_url;
	private SeekBar seekBar;
	private boolean isChanging;
	private Timer mTimer;
	private TimerTask mTimerTask;
	private TextView time, play_time;
	// //线程加载进度 圈圈
//	private ProgressBar progressvisitbar;
	private ProgressDialog pdialog;
	private int flog=0;
	private Handler handler;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.video_play);
		initView();
	}


	private void initView() {
		pdialog = ProgressDialog.show(VideoPlayActivity.this, "提示", "加载中......");
		findViewById(R.id.play).setOnClickListener(this);
		findViewById(R.id.pause).setOnClickListener(this);
		findViewById(R.id.stop).setOnClickListener(this);
		surface = (SurfaceView) findViewById(R.id.surfaceview);
		seekBar = (SeekBar) findViewById(R.id.SeekBar);
		seekBar.setOnSeekBarChangeListener(new MySeekbar());
		time = (TextView) findViewById(R.id.time);
		play_time = (TextView) findViewById(R.id.play_time);
		surfaceHolder = surface.getHolder();// SurfaceHolder是SurfaceView的控制接口
		surfaceHolder.addCallback(this);// 因为这个类实现了SurfaceHolder.Callback接口，所以回调参数直接this
		// surfaceHolder.setFixedSize(320, 220);//显示的分辨率,不设置为视频默认
		surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);// Surface类型
		video_url = getIntent().getExtras().getString("url");


		 handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				switch (msg.what) {
				case 1:
					pdialog.dismiss();

					break;
				}
			}
		};
	}

	@Override
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
	}

	@Override
	public void surfaceCreated(SurfaceHolder arg0) {
		// 必须在surface创建后才能初始化MediaPlayer,否则不会显示图像
		player = new MediaPlayer();
		player.setAudioStreamType(AudioManager.STREAM_MUSIC);
		player.setDisplay(surfaceHolder);
		// 设置显示视频显示在SurfaceView上
		try {
			player.setDataSource(video_url);
			player.prepare();
			int intime = player.getDuration() / 1000;
			time.setText(intime / 60 + "分" + intime % 60 + "秒");
			seekBar.setMax(player.getDuration());// 设置进度条
			// ----------定时器记录播放进度---------//
			mTimer = new Timer();
			mTimerTask = new TimerTask() {
				@Override
				public void run() {
					Log.i(TAG, "Running!");
					if (isChanging == true) {
						return;
					}
					if (player != null && player.isPlaying()) {
						seekBar.setProgress(player.getCurrentPosition());
						dismissdialog();
					}

				}
			};
			mTimer.schedule(mTimerTask, 0, 10);

		} catch (Exception e) {
			e.printStackTrace();
		}

		if (player.isPlaying()) {
			player.stop();
		}
		
		playerThread();
	}

	private void dismissdialog() {
		if(pdialog!=null&&pdialog.isShowing()){
			pdialog.dismiss();
		}
	}
	private void playerThread() {

		new Thread(new Runnable() {
			@Override
			public void run() {
				
				player.start();
			}
		}).start();
	}
	
	// 进度条处理
	class MySeekbar implements OnSeekBarChangeListener {
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			int inTime = progress / 1000;
			String strTime = inTime / 60 + "分" + inTime % 60 + "秒";
			if (!play_time.getText().toString().equals(strTime)) {
				Log.i(TAG, strTime);
				play_time.setText(strTime);
			}
		}

		public void onStartTrackingTouch(SeekBar seekBar) {
			isChanging = true;
		}

		public void onStopTrackingTouch(SeekBar seekBar) {
			player.seekTo(seekBar.getProgress());
			isChanging = false;
		}

	}

	@Override
	public void surfaceDestroyed(SurfaceHolder arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		//一定要把定时器取消了，不然会报错
		if (mTimer!=null) {
			mTimer.cancel();
		}
		if (player!=null&&player.isPlaying()) {
			player.stop();
			player.release();
		}
		
		
		// Activity销毁时停止播放，释放资源。不做这个操作，即使退出还是能听到视频播放的声音
	}

	@Override
	public void onClick(View view) {

		switch (view.getId()) {
		case R.id.play:
			if (player.isPlaying()) {
				player.stop();
			}
			player.start();
			break;

		case R.id.pause:
			player.pause();
			break;

		default:
			break;
		}
	}
}
